﻿using Domain.Entitys.Sinv;
using Domain.IRepositorys.Sinv;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys.Sinv
{
    public class Sinv_CustomerRepository : ISinv_CustomerRepository
    {
        private readonly MyDbContext myDbContext;

        public Sinv_CustomerRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        /// <summary>
        /// 分页条件查询客户信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<List<Sinv_Customer>> GetSinv_CustomerAsync(string name, string phone, int currentPage, int pageSize)
        {
            IQueryable<Sinv_Customer> query = myDbContext.Sinv_Customer.Where(c => c.Del_flag == '0');
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(c => c.Contacts_name.Contains(name) || c.Customer_code.Contains(name));
            }
            if (!string.IsNullOrEmpty(phone))
            {
                query = query.Where(c => c.Contacts_phone.Contains(phone));
            }
            return await query.OrderByDescending(c => c.Create_time).Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        /// <summary>
        /// 条件查询客户总数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public async Task<int> GetSinv_CustomerCountAsync(string name, string phone)
        {
            IQueryable<Sinv_Customer> query = myDbContext.Sinv_Customer.Where(c => c.Del_flag == '0');
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(c => c.Contacts_name.Contains(name) || c.Customer_code.Contains(name));
            }
            if (!string.IsNullOrEmpty(phone))
            {
                query = query.Where(c => c.Contacts_phone.Contains(phone));
            }
            return await query.CountAsync();
        }

        /// <summary>
        /// 逻辑删除客户信息
        /// </summary>
        /// <param name="sinv_Customer"></param>
        /// <returns></returns>
        public async Task<int> Sinv_CustomerDelAsync(Sinv_Customer sinv_Customer)
        {
            Sinv_Customer sinv_Customer1 = myDbContext.Sinv_Customer.FirstOrDefault(c => c.Customer_id == sinv_Customer.Customer_id);
            sinv_Customer1.Del_flag = sinv_Customer.Del_flag;
            myDbContext.Sinv_Customer.Update(sinv_Customer1);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="sinv_Customer"></param>
        /// <returns></returns>
        public async Task<int> Sinv_CustomerAddAsync(Sinv_Customer sinv_Customer)
        {
            await myDbContext.Sinv_Customer.AddAsync(sinv_Customer);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据客户编号查询对应客户信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<Sinv_Customer> Sinv_CustomerByCodeAsync(string code)
        {
            return await myDbContext.Sinv_Customer.FirstOrDefaultAsync(c => c.Customer_code == code);
        }

        /// <summary>
        /// 修改客户信息
        /// </summary>
        /// <param name="sinv_Customer"></param>
        /// <returns></returns>
        public async Task<int> Sinv_CustomerUpdateAsync(Sinv_Customer sinv_Customer)
        {
            Sinv_Customer sinv_Customer1 = myDbContext.Sinv_Customer.FirstOrDefault(c => c.Customer_id == sinv_Customer.Customer_id);
            sinv_Customer1.Contacts_name = sinv_Customer.Contacts_name;
            sinv_Customer1.Customer_code = sinv_Customer.Customer_code;
            sinv_Customer1.Customer_status = sinv_Customer.Customer_status;
            sinv_Customer1.Customer_source = sinv_Customer.Customer_source;
            sinv_Customer1.Customer_level = sinv_Customer.Customer_level;
            sinv_Customer1.Customer_industry = sinv_Customer.Customer_industry;
            sinv_Customer1.Customer_email = sinv_Customer.Customer_email;
            sinv_Customer1.Customer_address = sinv_Customer.Customer_address;
            sinv_Customer1.Customer_details = sinv_Customer.Customer_details;
            sinv_Customer1.Customer_personCharge = sinv_Customer.Customer_personCharge;
            sinv_Customer1.Dept_id = sinv_Customer.Dept_id;
            sinv_Customer1.Update_by = sinv_Customer.Update_by;
            sinv_Customer1.Update_time = sinv_Customer.Update_time;
            sinv_Customer1.Customer_name = sinv_Customer.Customer_name;
            sinv_Customer1.Contacts_phone = sinv_Customer.Contacts_phone;
            sinv_Customer1.Contacts_dept = sinv_Customer.Contacts_dept;
            sinv_Customer1.Contacts_position = sinv_Customer.Contacts_position;
            myDbContext.Sinv_Customer.Update(sinv_Customer1);
            return await myDbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据客户名称查询客户信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<List<Sinv_Customer>> Sinv_CustomerByNameAsync(string name)
        {
            var query = myDbContext.Sinv_Customer.Where(c => c.Del_flag == '0');
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(c => c.Customer_name.Contains(name));
            }
            return await query.ToListAsync();
        }
    }
}
